package com.team9.fitness.controller;

import com.team9.fitness.entity.UserProfile;
import com.team9.fitness.service.ZRDDSSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

/**
 * 用户个人信息管理控制器
 */
@RestController
@RequestMapping("/api/user-profile")
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
@Slf4j
public class UserProfileController {

    @Autowired
    private ZRDDSSendService zrddsSendService;

    /**
     * 保存或更新用户个人信息
     */
    @PostMapping("/save")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> saveUserProfile(@RequestBody UserProfile userProfile) {
        
        log.info("保存用户个人信息请求: userId={}", userProfile.getUserId());
        
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("userProfile", userProfile);
        
        return zrddsSendService.sendBusinessRequest("USER_PROFILE_SAVE", "POST", "/api/user-profile/save", 
                userProfile.getUserId().toString(), requestBody)
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "个人信息保存成功");
                        result.put("data", response.getData());
                        log.info("个人信息保存成功: userId={}", userProfile.getUserId());
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "个人信息保存失败");
                        log.warn("个人信息保存失败: userId={}, error={}", userProfile.getUserId(), response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("个人信息保存异常: userId={}", userProfile.getUserId(), throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "个人信息保存服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }

    /**
     * 获取用户个人信息
     */
    @GetMapping("/{userId}")
    public CompletableFuture<ResponseEntity<Map<String, Object>>> getUserProfile(@PathVariable Long userId) {
        
        log.info("获取用户个人信息请求: userId={}", userId);
        
        return zrddsSendService.sendBusinessRequest("USER_PROFILE_GET", "GET", "/api/user-profile/" + userId, 
                userId.toString(), new HashMap<>())
                .thenApply(response -> {
                    Map<String, Object> result = new HashMap<>();
                    if (response.getSuccess()) {
                        result.put("success", true);
                        result.put("message", "获取个人信息成功");
                        result.put("data", response.getData());
                        log.info("获取个人信息成功: userId={}", userId);
                    } else {
                        result.put("success", false);
                        result.put("message", response.getMessage() != null ? response.getMessage() : "获取个人信息失败");
                        log.warn("获取个人信息失败: userId={}, error={}", userId, response.getError());
                    }
                    return ResponseEntity.ok(result);
                })
                .exceptionally(throwable -> {
                    log.error("获取个人信息异常: userId={}", userId, throwable);
                    Map<String, Object> result = new HashMap<>();
                    result.put("success", false);
                    result.put("message", "获取个人信息服务异常，请稍后重试");
                    return ResponseEntity.ok(result);
                });
    }
}
